﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.VisualStudio.XmlEditor;

namespace CodeCounsel.SharePoint.RibbonDesigner.Model
{
    [XName(Name="CommandUIDefinition", Namespace=Namespaces.CommandUIText)]
    public class CommandUIDefinition
        : DomainElement
    {
        public CommandUIExtensionType Type
        {
            get { return Location.IsReplacementId ? CommandUIExtensionType.Replace : CommandUIExtensionType.Append; }
        }

        public DomainElement Content
        {
            get { return Children.FirstOrDefault(); }
        }

        public RibbonId Location
        {
            get { return DataModel.GetAttributeValue<RibbonId>(Schema.Location, null); }
            set { DataModel.SetAttributeValue<RibbonId>(Schema.Location, value); }
        }

        public CommandUIDefinition(XElement dataModel, DomainModelSource source)
            : base(dataModel, source)
        {
        }

        public CommandUIDefinition()
        {
        }

        protected override void RaiseDomainElementRemoved(DomainElement item)
        {
            OnContentRemoved(new DomainElementEventArgs<DomainElement>(item));
        }

        protected virtual void OnContentRemoved(DomainElementEventArgs<DomainElement> e)
        {
            if (ContentRemoved != null)
            {
                ContentRemoved(this, e);
            }
        }

        public event EventHandler<DomainElementEventArgs<DomainElement>> ContentRemoved;
    }
}